package com.jd.sys.service.impl;

import com.jd.sys.domain.Menu;
import com.jd.sys.mapper.MenuMapper;
import com.jd.sys.service.IMenuService;
import com.jd.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author É°Ç³
 * @since 2023-03-09
 */
@Service
public class MenuServiceImpl extends BaseServiceImpl<Menu> implements IMenuService {


    @Override
    public List<Menu> getFirst() {
        return super.findAll().stream().filter(menu -> Objects.nonNull(menu.getParentId())).collect(Collectors.toList());

    }


    @Override
    public List<Menu> getTree() {
        ArrayList<Menu> result = new ArrayList<>();
        List<Menu> all = super.findAll();
        Map<Long, Menu> collect = all.stream().collect(Collectors.toMap(Menu::getId, menu -> menu));
        for (Menu menu : all) {
            // 判断有没有parentId 确定是不是顶级菜单
            if (menu.getParentId()==null){
                result.add(menu);
            }else {
                Menu parent = collect.get(menu.getParentId());
                parent.getChildren().add(menu);
            }
        }
        System.err.println("菜单" + result);
        return result;
    }
}
